<?php


function flex_finish( $flex_res) {
	global $admin;
	$xml = new flex_xml;
	$xml->main_node= "node";
	$xml->data=  $flex_res  ;
	$xml->print_flex_xml();
	return true;
}

function flex_tree() {
	global $admin;
	$globxml = new flex_xml;
	$globxml->main_node= "adminTree";
	$myxml = $globxml->print_flex_header();
	
	// root - applications
	$data=  $admin->perm->getApplications()  ;
	foreach( $data as $k => $v ) {
    	$txt2 = $globxml->xml->createElement("node");
	$txt2 = $myxml->appendChild($txt2);
	
	    $txt2->setAttribute('label', $v['application_define_name']." (id ".$v['application_id'].")" );
	    $txt2->setAttribute('app_id', $v['application_id'] );
	    $txt2->setAttribute('app', 'app' );
	    $txt2->setAttribute('icon', 'iconApp' );
	    
	    
	    //Adding areas
	    $filterArea = array('filters' => array('application_id' => $v['application_id']));
	    $dataArea =  $admin->perm->getAreas( $filterArea );
	    foreach( $dataArea as $k2 => $v2 ) {
    		    $txt3 = $globxml->xml->createElement("node");
		    $txt3 = $txt2->appendChild($txt3);
		    $txt3->setAttribute('label', $v2['area_define_name']." (id ".$v2['area_id'].")" );
    		    $txt3->setAttribute('app', 'area' );
		    $txt3->setAttribute('icon', 'iconArea' );
		    $txt3->setAttribute('area_id', $v2['area_id'] );
		    //$txt3->setAttribute('application_id', $v2['application_id'] );
		    
			//Adding rights
			$filterRight = array('filters' => array('area_id' => $v2['area_id']));
			$dataRight =  $admin->perm->getRights( $filterRight );
			foreach( $dataRight as $k3 => $v3 ) {
    				$txt4 = $globxml->xml->createElement("node");
				$txt4 = $txt3->appendChild($txt4);
				$txt4->setAttribute('right_id', $v3['right_id'] );
				$txt4->setAttribute('icon', 'iconRight' );
				$txt4->setAttribute('app', 'right' );
				$txt4->setAttribute('label', $v3['right_define_name'] );
				$txt4->setAttribute('right_define_name', $v3['right_define_name'] );
				//Adding groups
				$filterGroup = array('filters' => array('right_id' => $v3['right_id']));
				$dataGroup =  $admin->perm->getGroups( $filterGroup );
    				foreach( $dataGroup as $k4 => $v4 ) {
    					$txt5 = $globxml->xml->createElement("node");
					$txt5 = $txt4->appendChild($txt5);
					$txt5->setAttribute('label', $v4['group_define_name'] );
					$txt5->setAttribute('right_id', $v3['right_id'] );
					$txt5->setAttribute('right_define_name', $v3['right_define_name'] );
					$txt5->setAttribute('app', 'group' );
					$txt5->setAttribute('icon', 'iconGroup' );
					$txt5->setAttribute('group_id', $v4['group_id'] );
					//Adding users
					$filterUser = array('filters' => array('group_id' => $v4['group_id']));
					$dataUser =  $admin->getUsers( $filterUser );
					foreach( $dataUser as $k5 => $v5 ) {
    						$txt6 = $globxml->xml->createElement("node");
						$txt6 = $txt5->appendChild($txt6);
						$txt6->setAttribute('label', $v5['handle'] );
						$txt6->setAttribute('group_id', $v4['group_id'] );
						$txt6->setAttribute('group_define_name', $v4['group_define_name'] );
						$txt6->setAttribute('icon', 'iconUser' );
						$txt6->setAttribute('app', 'groupuser' );
						$txt6->setAttribute('auth_user_id', $v5['handle'] );
						
					}
				} 
				//Adding users also without group clasification
				$filterUser = array('filters' => array('right_id' => $v3['right_id']));
				$dataUser =  $admin->getUsers( $filterUser );
				foreach( $dataUser as $k5 => $v5 ) {
    					$txt7 = $globxml->xml->createElement("node");
					$txt7 = $txt4->appendChild($txt7);
					//print_r($v5);
					$txt7->setAttribute('label', $v5['handle'] );
					$txt7->setAttribute('right_id', $v3['right_id'] );
					$txt7->setAttribute('right_define_name', $v3['right_define_name'] );
					$txt7->setAttribute('app', 'user' );
					$txt7->setAttribute('icon', 'iconUser' );
					$txt7->setAttribute('auth_user_id', $v5['handle'] );
					
				}

				

			}
	    }
									
	}              
	
	$globxml->print_flex_footer();
 	return true;

}

?>
